package com.epfresh.supplier.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.OrderOnGroupMinusDetail;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class OrderOnGroupMinusDetailActivity extends BaseActivity {

    static final String TAG = "OrderMinusDetailActivity";
    TextView tvState,tvMoneyTop, tvTime, tvReason,tvMoneyWay;
    View tvInfoHint;
    TextView tvStarter, tvMoneyBottom,tvNumber,tvMinusReason,tvId,tvApplyTime, tvInfo;

    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        id = getIntent().getStringExtra("id");
        if(id!=null){
            requestOrderInfo();
        }else {
            T.toast("没收到订单号");
            finish();
        }
    }
    void initView(){
        setContentView(R.layout.activity_order_on_group_minis_detail);
        tvState = (TextView)findViewById(R.id.tv_state);
        tvMoneyTop = (TextView)findViewById(R.id.tv_money_top);
        tvTime = (TextView)findViewById(R.id.tv_time);
        tvReason = (TextView)findViewById(R.id.tv_reason);
        tvMoneyWay = (TextView)findViewById(R.id.tv_money_way);

        tvStarter = (TextView)findViewById(R.id.tv_starter);

        tvMoneyBottom = (TextView)findViewById(R.id.tv_money_bottom);
        tvNumber = (TextView)findViewById(R.id.tv_number);
        tvMinusReason = (TextView)findViewById(R.id.tv_minus_reason);
        tvId = (TextView)findViewById(R.id.tv_id);
        tvApplyTime = (TextView)findViewById(R.id.tv_apply_time);
        tvInfo = (TextView)findViewById(R.id.tv_info);
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);

        tvInfoHint = findViewById(R.id.tv_info_hint);

        rightbtn.setOnClickListener(this);
        rightbtn.setText("帮助");
        rightbtn.setVisibility(View.VISIBLE);
        toolbarTitle.setText("退款详情");
    }

    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.baseback:
                // 返回上一页
                finish();
                break;

            case R.id.rightbtn:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_REDUCTION);
                intent.putExtra(Constant.KEY_WEB_TITLE, getString(R.string.help));
                startActivity(intent);
                break;
        }
    }


    OnRequestListener<OrderOnGroupMinusDetail> onRequestListener = new OnRequestListener<OrderOnGroupMinusDetail>() {
        @Override
        public OrderOnGroupMinusDetail jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, OrderOnGroupMinusDetail.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            showExceptionView(R.mipmap.upload_error, "服务器出现小错误，请稍后再试", HttpRequest.FAIL_NET_FAULT);
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
        }
        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<OrderOnGroupMinusDetail> response, Object tag) {
            hideProgressDialog();
            OrderOnGroupMinusDetail responseElement = response.getResponseElement();
            updateOrderInfo(responseElement);
        }
    };
    String cmdOrderDetails = "pm-order/baseorder/order/jmt/detail";

    public void requestOrderInfo() {
        RequestEntitySupply<Map<String, String>> entity = new RequestEntitySupply<>();
        entity.setCmd(cmdOrderDetails);
        Map<String, String> map = new HashMap<>();
        map.put("orderJmtId",id);
        entity.setParameters(map);
        request(entity, cmdOrderDetails, onRequestListener);
    }
    OrderOnGroupMinusDetail.JmtIntoBean data;

    public void updateOrderInfo(OrderOnGroupMinusDetail bean) {
        this.data = bean.getJmtInto();
        tvState.setText(data.getStatusTitle());
        if(data.getRefundSuccessDate()==null||"".equals(data.getRefundSuccessDate())||data.getRefundSuccessDate().equals("null")) {
            tvTime.setVisibility(View.GONE);
        }else{
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText("退款时间： " + data.getRefundSuccessDate());
        }
        if(data.getRefundPrice()==null||"".equals(data.getRefundPrice())||data.getRefundPrice().equals("null"))
            tvMoneyTop.setVisibility(View.GONE);
        if(data.getRemark()==null||"".equals(data.getRemark())||data.getRemark().equals("null"))
            data.setRemark("");
        if(data.getApplyDate()==null||"".equals(data.getApplyDate())||data.getApplyDate().equals("null"))
            data.setApplyDate("");
        /*if(data.getRefundPrice()==null||"".equals(data.getRefundPrice())||data.getRefundPrice().equals("null"))
            data.setRefundPrice("0");
        if(data.getRefundPrice()==null||"".equals(data.getRefundPrice())||data.getRefundPrice().equals("null"))
            data.setRefundPrice("0");*/
        tvMoneyTop.setText("退款金额："+data.getRefundPrice()+"元");
/*        if("40".equals(data.getStatus())){
            tvMoneyTop.setVisibility(View.GONE);
        }*/
        tvStarter.setText("发起方："+data.getApplicant());
        tvMinusReason.setText("退款原因："+data.getAppleReasonTitle());
        tvMoneyBottom.setText("退款金额："+data.getApplyPrice()+"元");

        tvInfo.setText("退款说明："+data.getRemark());
        tvId.setText("申请编号："+data.getJmtNo());
        tvApplyTime.setText("申请时间："+data.getApplyDate());
    }
}
